--冥渊終焉龍皇·万灵帰骸の主
local s,id=GetID()
function s.initial_effect(c)
	--
	local e0=Effect.CreateEffect(c)
	e0:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e0:SetCode(EVENT_STARTUP)
	e0:SetRange(LOCATION_ALL)
	e0:SetProperty(EFFECT_FLAG_UNCOPYABLE+EFFECT_FLAG_CANNOT_DISABLE)
	e0:SetOperation(s.op)
	c:RegisterEffect(e0)
	--
	local e1=Effect.CreateEffect(c)
	e1:SetCategory(CATEGORY_TOGRAVE+CATEGORY_REMOVE)
	e1:SetType(EFFECT_TYPE_ACTIVATE)
	e1:SetCode(EVENT_FREE_CHAIN)
	e1:SetTarget(s.target)
	e1:SetOperation(s.operation)
	c:RegisterEffect(e1)
	--
	local e2=Effect.CreateEffect(c)
	e2:SetType(EFFECT_TYPE_IGNITION)
	e2:SetProperty(EFFECT_FLAG_CANNOT_DISABLE+EFFECT_FLAG_CANNOT_INACTIVATE+EFFECT_FLAG_CANNOT_NEGATE)
	e2:SetRange(LOCATION_GRAVE+LOCATION_REMOVED)
	e2:SetCondition(function(e) return s[e:GetHandlerPlayer()]>=12 end)
	e2:SetTarget(function(e) return Duel.GetFlagEffect(e:GetHandlerPlayer(),id)==0 end)
	e2:SetOperation(s.activate)
	c:RegisterEffect(e2)  
	--eff grant
	--immune
	local e3=Effect.CreateEffect(c)
	e3:SetType(EFFECT_TYPE_FIELD)
	e3:SetCode(EFFECT_IMMUNE_EFFECT)
	e3:SetRange(0xff)
	e3:SetTargetRange(LOCATION_MZONE,0)
	e3:SetCondition(function(e) return Duel.GetFlagEffect(e:GetHandlerPlayer(),id)~=0 end)
	e3:SetTarget(aux.TargetBoolFunction(Card.IsCode,33533619))
	e3:SetValue(s.efilter)
	c:RegisterEffect(e3)
	--Cannot be used as material for a Fusion/Synchro/Xyz/Link Summon
	local e4=e3:Clone()
	e4:SetType(EFFECT_TYPE_FIELD)
	e4:SetCode(EFFECT_CANNOT_BE_MATERIAL)
	e4:SetValue(aux.cannotmatfilter(SUMMON_TYPE_XYZ,SUMMON_TYPE_LINK))
	c:RegisterEffect(e4)
	--cannot release
	local e5=e3:Clone()
	e5:SetType(EFFECT_TYPE_FIELD)
	e5:SetRange(0xff)
	e5:SetCode(EFFECT_UNRELEASABLE_SUM)
	e5:SetValue(1)
	c:RegisterEffect(e5)
	local e6=e5:Clone()
	e6:SetCode(EFFECT_UNRELEASABLE_NONSUM)
	c:RegisterEffect(e6)
	--negate
	local e7=Effect.CreateEffect(c)
	e7:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
	e7:SetRange(0xff)
	e7:SetCode(EVENT_CHAINING)
	e7:SetCondition(s.atkcon)
	e7:SetOperation(s.atkop)
	c:RegisterEffect(e7)


	if not s.global_check then
		s.global_check=true
		s[0]=0
		s[1]=0
		local ge0=Effect.CreateEffect(c)
		ge0:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge0:SetCode(EVENT_SPSUMMON_SUCCESS)
		ge0:SetOperation(s.chk)
		Duel.RegisterEffect(ge0,0)
		local ge1=Effect.CreateEffect(c)
		ge1:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge1:SetCode(EVENT_CHAINING)
		ge1:SetOperation(s.aclimit1)
		Duel.RegisterEffect(ge1,0)
		local ge2=Effect.CreateEffect(c)
		ge2:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge2:SetCode(EVENT_CHAIN_NEGATED)
		ge2:SetOperation(s.aclimit2)
		Duel.RegisterEffect(ge2,0)
		local ge3=Effect.CreateEffect(c)
		ge3:SetType(EFFECT_TYPE_FIELD+EFFECT_TYPE_CONTINUOUS)
		ge3:SetCode(EVENT_BATTLE_DESTROYING)
		ge3:SetOperation(s.chk2)
		Duel.RegisterEffect(ge3,0)
	end 
end
s.listed_names={33533619}
function s.chk(e,tp,eg,ep,ev,re,r,rp)
	for p=0,1 do
		local tc=eg:GetFirst()
		while tc do
			if tc:IsSummonPlayer(p) and tc:IsCode(33533619) then
				s[p]=s[p]+1
			end
			tc=eg:GetNext()
		end
	end
end
function s.aclimit1(e,tp,eg,ep,ev,re,r,rp)
	local rc=re:GetHandler()
	if not rc:IsCode(33533619) then return end
	if ep==0 then s[0]=s[0]+1 end
	if ep==1 then s[1]=s[1]+1 end
end
function s.aclimit2(e,tp,eg,ep,ev,re,r,rp)
	local rc=re:GetHandler()
	if not rc:IsCode(33533619) then return end
	if ep==0 then s[0]=s[0]-1 end
	if ep==1 then s[1]=s[1]-1 end  
end
function s.chkfilter(c,tp)
	if not c:GetBattleTarget():IsPreviousControler(1-tp) then return false end
	if c:IsRelateToBattle() then
		return c:IsControler(tp) and c:IsCode(33533619)
	else
		return c:IsPreviousControler(tp) and c:IsPreviousCode(33533619)
	end
end
function s.chk2(e,tp,eg,ep,ev,re,r,rp)
	for p=0,1 do
		if eg:IsExists(s.chkfilter,1,nil,p) then
			s[p]=s[p]+1
		end
	end
end
function s.op(e,tp,eg,ep,ev,re,r,rp)
	local c = e:GetOwner()
	local p = c:GetControler()
	if Duel.SelectYesNo(tp,aux.Stringid(id,0)) then
		Duel.Hint(HINT_CARD,tp,id)
		Duel.DisableShuffleCheck()
		local ct = Duel.GetMatchingGroupCount(nil,p,LOCATION_HAND+LOCATION_DECK,0,c)
		if (Duel.IsDuelType(DUEL_MODE_SPEED) and ct < 20 or ct < 40) and Duel.SelectYesNo(1-p, aux.Stringid(4014,5)) then
			Duel.Win(1-p,0x55)
		end
		if c and c:IsAbleToGrave() and (not c:IsAbleToRemove() or Duel.SelectYesNo(tp,aux.Stringid(62834295,2))) then
			Duel.SendtoGrave(c,REASON_RULE)
		else
			Duel.Remove(c,POS_FACEUP,REASON_RULE)
		end
	end
	e:Reset()
end
function s.target(e,tp,eg,ep,ev,re,r,rp,chk)
	local c=e:GetHandler()
	if chk==0 then return c:IsAbleToGrave() or c:IsAbleToRemove() end
	Duel.SetPossibleOperationInfo(0,CATEGORY_TOGRAVE,c,1,tp,LOCATION_HAND)
	Duel.SetPossibleOperationInfo(0,CATEGORY_REMOVE,c,1,tp,LOCATION_HAND)
end
function s.operation(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	if c:IsRelateToEffect(e) and c:IsAbleToGrave() and (not c:IsAbleToRemove() or Duel.SelectYesNo(tp,aux.Stringid(62834295,2))) then
		Duel.SendtoGrave(c,REASON_EFFECT)
	else
		Duel.Remove(c,POS_FACEUP,REASON_EFFECT)
	end
end
function s.activate(e,tp,eg,ep,ev,re,r,rp)
	Duel.RegisterFlagEffect(tp,id,0,0,1)
	local c=e:GetHandler()
	local e1=Effect.CreateEffect(c)
	e1:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
	e1:SetType(EFFECT_TYPE_FIELD)
	e1:SetTargetRange(LOCATION_ALL,LOCATION_ALL)
	e1:SetTarget(aux.TargetBoolFunction(Card.IsMonster))
	e1:SetCode(EFFECT_CHANGE_ATTRIBUTE)
	e1:SetValue(ATTRIBUTE_DARK|ATTRIBUTE_LIGHT)
	Duel.RegisterEffect(e1,tp)
	local e2=Effect.CreateEffect(c)
	e2:SetProperty(EFFECT_FLAG_SET_AVAILABLE)
	e2:SetType(EFFECT_TYPE_FIELD)
	e2:SetTargetRange(LOCATION_ALL,LOCATION_ALL)
	e2:SetTarget(aux.TargetBoolFunction(Card.IsMonster))
	e2:SetTargetRange(LOCATION_ALL,LOCATION_ALL)
	e2:SetCode(EFFECT_CHANGE_RACE)
	e2:SetValue(RACE_DRAGON)
	Duel.RegisterEffect(e2,tp)
end
function s.efilter(e,re)
	return e:GetOwnerPlayer()==1-re:GetOwnerPlayer()
end
function s.atkcon(e,tp,eg,ep,ev,re,r,rp)
	return re:GetActivateLocation()==LOCATION_GRAVE and Duel.GetFlagEffect(tp,id)~=0 and Duel.IsExistingMatchingCard(aux.FaceupFilter(Card.IsCode,33533619),tp,LOCATION_MZONE,0,1,nil)
end
function s.atkop(e,tp,eg,ep,ev,re,r,rp)
	local c=e:GetHandler()
	local g=Duel.GetMatchingGroup(aux.FaceupFilter(Card.IsCode,33533619),tp,LOCATION_MZONE,LOCATION_MZONE,nil)
	for tc in aux.Next(g) do
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_UPDATE_ATTACK)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetValue(200)
		e1:SetReset(RESET_EVENT|RESETS_STANDARD)
		tc:RegisterEffect(e1)
	end
	local mg=Duel.GetMatchingGroup(s.atkfilter,tp,LOCATION_MZONE,LOCATION_MZONE,nil)
	local ng=Group.CreateGroup()
	for nc in aux.Next(mg) do
		local preatk=nc:GetAttack()
		local e1=Effect.CreateEffect(c)
		e1:SetType(EFFECT_TYPE_SINGLE)
		e1:SetCode(EFFECT_UPDATE_ATTACK)
		e1:SetProperty(EFFECT_FLAG_CANNOT_DISABLE)
		e1:SetValue(-200)
		e1:SetReset(RESET_EVENT|RESETS_STANDARD)
		nc:RegisterEffect(e1)
		if preatk<2500 and nc:IsAttackBelow(2500) then ng:AddCard(nc) end
	end
	if #ng==0 then return end
	for ac in aux.Next(ng) do
		ac:NegateEffects(c)
	end
end
function s.atkfilter(c)
	return not c:IsCode(33533619) and c:IsFaceup()
end